<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="./js/vue.js"></script>
  <style>
      #app{
          width: 1200px;
          display: flex;
          align-items: center;
          justify-content:space-evenly;
      }

      li{
          list-style: none;
          width: 1000px;
          display: flex;
          align-items: center;
          justify-content:space-between;
      }
  </style>
</head>

<body>
 
  <div id="app">
    <ul>
    <li>
   <span><input type="checkbox" v-model="allChecked" @change="all">全选</span>
   <span style="padding-left: -50px;">商品</span>
   <span>价格</span>
   <span>数量</span>
   <span></span>
   </li>
      <li v-for="item,index in carts" :key="item.id">
        <span><input type="checkbox" :value="item.id" v-model="ids" @change="changeItem"></span> 
        <span><img :src="item.pic" alt="" style="width:50px;"></span>
        <span>{{item.name}}</span>
        <span>￥：{{item.price}}</span>
        <span>
          <button @click="decr(item)">-</button>
          <input type="number" v-model.number="item.num" @input="inputFn">
          <button @click="incr(item)">+</button>
          
        </span>
        <button @click="del(index)">删除</button>
      </li>
      <span v-if="allChecked" @click="delAll">删除全部</span>

      <div>合计：{{totalPrice()}}</div>
    </ul>
 

  </div>
  <script>

    new Vue({
      el: '#app',
      data: {
        allChecked: false,
        ids: [],
        carts: [
          { id: 1, pic: 'https://img14.360buyimg.com/babel/s320x320_jfs/t1/121591/31/27770/108029/6264bf67E2566526f/0f6456a0160c2dae.jpg!cc_320x320.webp', name: 'oppo', price: 1999, num: 1 },
          { id: 2, pic: 'https://img30.360buyimg.com/babel/s320x320_jfs/t1/106505/8/27719/73250/6266598fEc88b687d/f4b70b253080c37a.jpg!cc_320x320.webp', name: '小米', price: 1299, num: 1 },
          { id: 3, pic: 'https://img30.360buyimg.com/babel/s320x320_jfs/t1/197505/24/13599/83205/616dc631E854d2563/e98b96e5044af9da.jpg!cc_320x320.webp', name: '华为', price: 2999, num: 1 }
        ]
      },
      methods: {
        all(evt) {
          // console.log(evt.target.checked)
          if (evt.target.checked) { // 选中时
            this.ids = this.carts.map(({ id }) => id)
          } else {
            this.ids = []
          }
        },
        changeItem(evt) {
          if (!evt.target.checked) {
            this.allChecked = false
          } else {
            // 判断当前数据是否和源数据长度一致
            this.allChecked = this.arr.length == this.ids.length
          }
        },
        delAll(){
          this.carts = []
        },
        incr(item) {
          item.num = Math.min(10, ++item.num)
        },
        decr(item) {
          item.num = Math.max(1, --item.num)
        },
        totalPrice() {
        let total=this.carts.reduce((total,t)=>{
            total+=t.num*t.price
            return total
        },0)
        return total
        },
        del(index){
            this.carts.splice(index, 1)
        },
        inputFn(e){
            console.log(e.target.value);
            if(e.target.value>10){
                // return 
                e.target.value=10
            }else if(e.target.value<1){
                e.target.value=1
            }
            // else{
            //   return  e.target.value
            // }
        }
      }
    })
  </script>
</body>

</html>